﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.Reflection;
using System;

namespace Sprinkles.Secondary
{
    public class PropertyValueSetter<A, T>
    {
        private readonly Deployment<T> m_Owner;
        private readonly PropertyInfo m_PropertyInfo;

        public Deployment<T> SetLiteral(A value)
        {
            m_Owner.AddPropertyValue(m_PropertyInfo, value);
            return m_Owner;
        }
        public Deployment<T> SetUntypedObject(object value)
        {
            m_Owner.AddPropertyValue(m_PropertyInfo, value);
            return m_Owner;
        }
        public Deployment<T> SetExpression(string expression)
        {
            var value = new SprinklesExpressionHolder(expression);
            m_Owner.AddPropertyValue(m_PropertyInfo, value);
            return m_Owner;
        }
        public Deployment<T> SetReference(string reference)
        {
            var value = new SprinklesRuntimeObjectReference(reference);
            m_Owner.AddPropertyValue(m_PropertyInfo, value);
            return m_Owner;
        }
        public Deployment<T> SetReference<B>(FutureDeployedObject<B> futureReference) where B : A
        {
            if (futureReference == null) throw new ArgumentNullException("futureReference");
            m_Owner.AddPropertyValue(m_PropertyInfo, futureReference);
            return m_Owner;
        }
        public Deployment<T> SetReferenceUntyped(IFutureDeployedObject futureReference)
        {
            if (futureReference == null) throw new ArgumentNullException("futureReference");
            m_Owner.AddPropertyValue(m_PropertyInfo, futureReference);
            return m_Owner;
        }
        public Deployment<T> SetAuto()
        {
            m_Owner.AddPropertyValue(m_PropertyInfo, new AutoReference());
            return m_Owner;
        }
        public Deployment<T> SetPlaceholder(string placeholder)
        {
            m_Owner.AddPropertyValue(m_PropertyInfo, placeholder);
            return m_Owner;
        }
        public PropertyValueSetter(Deployment<T> owner, PropertyInfo pi)
        {
            m_Owner = owner;
            m_PropertyInfo = pi;
        }
    }
}
